<?php
namespace App\Http\Controllers\Admin;

use App\Admin;
use App\Model\UserModel;
use App\Model\DepositModel;
use Illuminate\Http\Request;
use App\Model\AdminLogsModel;
use App\Model\UserWalletModel;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;

class DepositController extends Controller{
	//提现列表
	public function index(Request $request){
		$list = DepositModel::where(function($query){
			if(request()->keywords){
				$user = UserModel::where('phone',request()->keywords)->Orwhere('email',request()->keywords)->value('id');
				$query->where('uid',$user);
			}
			if(request()->sub_type){
				$query->where('type',request()->sub_type);
			}
		})
			->orderBy('id','desc')->paginate(10);

		return view('admin.deposit.index',compact('list','request'));
	}

	//审核通过
	public function auth_ok(){
		$auth_list = DepositModel::where('id',request()->id)->first();
		if(!$auth_list){
			return ['code'=>0,'msg'=>'非法入侵'];
		}

		$pass = env('CHECK_PASS');
		if($pass != request()->pass){
			return ['code'=>0,'msg'=>'操作密码输入有误!'];
		}
		DB::beginTransaction();
		try{
			DepositModel::where('id',request()->id)->update(['state'=>1]);
			DB::commit();
			$msg = '提现ID:'.request()->id.'(审核通过)';
			AdminLogsModel::addLog($msg);
			return ['code'=>1,'msg'=>'已通过'];
		}catch(Exception $e){
			DB::rollBack();
			return ['code'=>2,'msg'=>'操作失败'];

		}
	}

	//审核失败
	public function auth_no_remark(){
		$id = request()->id;
		return view('admin.deposit.check',compact('id'));
	}

	//审核失败
	public function auth_no(){
		if(!request()->remark){
			return ['code'=>0,'msg'=>'请输入驳回原因'];
		}
		DB::beginTransaction();
		try{
			$list = DepositModel::where('id',request()->id)->first();
			if(!$list){
				return ['code'=>0,'msg'=>'非法入侵'];
			}
			//返还现金
			UserWalletModel::handleAccount($list['uid'],$list['num'],'+','现金提现[后台驳回]',1,2,'deposit',request()->id);
			//更改提现状态
			DepositModel::where('id',request()->id)->update(['state'=>2,'remark'=>request()->remark]);
			DB::commit();
			$msg = '提现ID:'.request()->id.'(审核驳回)';
			AdminLogsModel::addLog($msg);
			return ['code'=>1,'msg'=>'操作成功'];
		}catch(Exception $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>'操作失败'];
		}
	}

	//审核成功-操作密码
	public function auth_ok_check(){
		$id = request()->id;
		return view('admin.deposit.check_ok',compact('id'));
	}


}